package kr.co.doriclassic.pro14;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.FlowLayout;

import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MileToKilometer extends JFrame implements ActionListener {
	private TrPanelInput panel_input;
	private JPanel panel_button;
	private TrPanelLabel panel_label;
	private final double KILOMETER=1.609344;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MileToKilometer frame = new MileToKilometer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MileToKilometer() {

		initialize();
	}
	private void initialize() {
		setTitle("마일을 킬로미터로 변환");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 185);
		getContentPane().setLayout(new BorderLayout(30, 10));
		
		panel_input = new TrPanelInput();
		FlowLayout flowLayout = (FlowLayout) panel_input.getLayout();
		flowLayout.setVgap(10);
		getContentPane().add(panel_input, BorderLayout.NORTH);
		
		panel_button = new JPanel();
		JButton btnResult = new JButton("변환");
		btnResult.setBackground(Color.BLUE);
		btnResult.setForeground(Color.WHITE);
		btnResult.addActionListener(this);
		panel_button.add(btnResult);
		panel_button.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		getContentPane().add(panel_button, BorderLayout.CENTER);
				
		panel_label = new TrPanelLabel();
		getContentPane().add(panel_label, BorderLayout.SOUTH);
		
		setResizable(false);
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		try {
			int mile = Integer.parseInt(panel_input.getTfInput().trim());
			String km = getKilometer(mile)+"";			
			panel_label.setTfResult(mile+" 마일은 "+km+"킬로미터 입니다.");	
		} catch (NumberFormatException e2) {
			JOptionPane.showMessageDialog(this, "숫자만 입력 가능합니다.");
		}	
	}

	private double getKilometer(int mile) {
		return mile * KILOMETER;  		
	}
}
